package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.vo.AddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.UserAddressMapper;
import com.ruoyi.system.domain.UserAddress;
import com.ruoyi.system.service.IUserAddressService;

/**
 * 用户配送地址Service业务层处理
 * 
 * @author Wxl
 * @date 2022-03-27
 */
@Service
public class UserAddressServiceImpl implements IUserAddressService 
{
    @Autowired
    private UserAddressMapper userAddressMapper;

    /**
     * 查询用户配送地址
     * 
     * @param id 用户配送地址主键
     * @return 用户配送地址
     */
    @Override
    public UserAddress selectUserAddressById(Long id)
    {
        return userAddressMapper.selectUserAddressById(id);
    }

    /**
     * 查询用户配送地址列表
     * 
     * @param userAddress 用户配送地址
     * @return 用户配送地址
     */
    @Override
    public List<UserAddress> selectUserAddressList(UserAddress userAddress)
    {
        return userAddressMapper.selectUserAddressList(userAddress);
    }

    /**
     * 新增用户配送地址
     * 
     * @param userAddress 用户配送地址
     * @return 结果
     */
    @Override
    public int insertUserAddress(UserAddress userAddress)
    {
        userAddress.setCreateTime(DateUtils.getNowDate());
        return userAddressMapper.insertUserAddress(userAddress);
    }

    /**
     * 修改用户配送地址
     * 
     * @param userAddress 用户配送地址
     * @return 结果
     */
    @Override
    public int updateUserAddress(UserAddress userAddress)
    {
        UserAddress user_id = userAddressMapper.selectOne(new QueryWrapper<UserAddress>()
                .eq("user_id", userAddress.getUserId())
                .eq("default_flag",1));
        user_id.setDefaultFlag(0);
        userAddressMapper.updateById(user_id);
        userAddress.setUpdateTime(DateUtils.getNowDate());
        return userAddressMapper.updateUserAddress(userAddress);
    }

    /**
     * 批量删除用户配送地址
     * 
     * @param ids 需要删除的用户配送地址主键
     * @return 结果
     */
    @Override
    public int deleteUserAddressByIds(Long[] ids)
    {
        return userAddressMapper.deleteUserAddressByIds(ids);
    }

    /**
     * 删除用户配送地址信息
     * 
     * @param id 用户配送地址主键
     * @return 结果
     */
    @Override
    public int deleteUserAddressById(Long id)
    {
        return userAddressMapper.deleteUserAddressById(id);
    }

    @Override
    public void saveOrUpdate(AddressVo userAddress) {
        Long id = userAddress.getId();
        UserAddress userAddress1 = userAddressMapper.selectUserAddressById(id);
        if (userAddress.getDefaultFlag()==1){
            UserAddress user_id = userAddressMapper.selectOne(new QueryWrapper<UserAddress>()
                    .eq("user_id", userAddress1.getUserId())
                    .eq("default_flag",1));
            if (user_id!=null){
                user_id.setDefaultFlag(0);
                userAddressMapper.updateById(user_id);
            }
        }

        if (userAddress1!=null){

            UserAddress oldAddress = new UserAddress();
            oldAddress.setId(userAddress.getId());
            oldAddress.setAddress(userAddress.getAddress());
            oldAddress.setUserId(userAddress.getUserId());
            oldAddress.setUsername(userAddress.getUserName());
            oldAddress.setArea(userAddress.getArea());
            oldAddress.setConsignee(userAddress.getName());
            oldAddress.setMobile(userAddress.getMobile());
            oldAddress.setDefaultFlag(userAddress.getDefaultFlag());
            userAddressMapper.updateById(oldAddress);
        }else{

            UserAddress newAddress = new UserAddress();
            newAddress.setId(new Date().getTime());
            newAddress.setAddress(userAddress.getAddress());
            newAddress.setUsername(userAddress.getUserName());
            newAddress.setUserId(userAddress.getUserId());
            newAddress.setArea(userAddress.getArea());
            newAddress.setConsignee(userAddress.getName());
            newAddress.setMobile(userAddress.getMobile());
            newAddress.setDefaultFlag(userAddress.getDefaultFlag());
            userAddressMapper.insert(newAddress);
        }
    }
}
